<template>
    <div>
        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
            <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
            <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>

        <div style="margin: 10px 0">
            <el-button type="primary" @click="generateRecord">生成执裁记录表 <i class="el-icon-circle-plus-outline"></i>
            </el-button>
            <!--      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>-->
            <!--      <el-popconfirm-->
            <!--          class="ml-5"-->
            <!--          confirm-button-text='确定'-->
            <!--          cancel-button-text='我再想想'-->
            <!--          icon="el-icon-info"-->
            <!--          icon-color="red"-->
            <!--          title="您确定批量删除这些数据吗？"-->
            <!--          @confirm="delBatch">-->
            <!--        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>-->
            <!--      </el-popconfirm>-->
            <!--      &lt;!&ndash; <el-upload action="http://localhost:9090/judge/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">-->
            <!--        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>-->
            <!--      </el-upload>-->
            <!--      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button> &ndash;&gt;-->
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
            <el-table-column prop="id" label="ID" width="100" v-if="false"></el-table-column>
<!--            <el-table-column prop="name" label="姓名" align="center" ></el-table-column>-->
            <el-table-column label="姓名" align="center">
                <template slot-scope="scope">
                    <div style="display: inline-block">
                        <el-image style="width: 60px; border-radius: 50%;
                        position: relative;height: 60px" :src="scope.row.avatar"
                                  :preview-src-list="[scope.row.avatar]"></el-image>
                        <span style="position:relative; bottom:13px;margin-left: 14px">{{ scope.row.name }}</span>
                    </div>

                </template>
            </el-table-column>
            <el-table-column prop="sex"  align="center" :formatter="sexFormatter"
                             label="性别"></el-table-column>
            <el-table-column prop="idcard"  align="center" label="身份证号"></el-table-column>
            <el-table-column prop="phone" align="center"  label="手机"></el-table-column>

<!--            <el-table-column prop="representUnit" width="100" align="center" label="代表单位"></el-table-column>-->
<!--            <el-table-column prop="workUnit" width="150" align="center" label="工作单位"></el-table-column>-->
<!--            <el-table-column prop="judgeLevel" width="100" align="center" :formatter="levelFormatter"-->
<!--                             label="现裁判等级"></el-table-column>-->
<!--            <el-table-column prop="passDate" width="120" align="center" label="现等级通过时间"></el-table-column>-->
<!--            <el-table-column prop="latestRecord" :show-overflow-tooltip="true" align="center"-->
<!--                             label="最近执裁或复训活动名称及地点"></el-table-column>-->
            <!--      <el-table-column prop="raceName" align="center"  :show-overflow-tooltip="true" label="报名赛事"></el-table-column>-->
            <!--      <el-table-column prop="raceId" align="center"  label="赛事id" v-if="false"></el-table-column>-->
            <!--      <el-table-column prop="isAccept" align="center" label="是否接受" :formatter="acceptFormatter"></el-table-column>-->
            <!--      <el-table-column prop="post"  align="center" label="选派岗位" :formatter="postFormatter"></el-table-column>-->

            <el-table-column label="操作" width="200px"  align="center">
                <template slot-scope="scope">
                    <el-tooltip class="item" effect="dark" content="查看执裁记录" placement="top-start">
                        <el-button type="text" @click="queryRecord(scope.row)"><i class="el-icon el-icon-view"></i>
                        </el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[10, 30, 50, 100]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog title="信息" :visible.sync="dialogFormVisible" width="40%" :close-on-click-modal="false">
            <el-form label-width="120px" size="small" style="width: 80%; margin: 0 auto">
                <el-form-item prop="name" label="姓名">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="sex" label="性别">
                    <el-input v-model="form.sex" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="idcard" label="身份证号">
                    <el-input v-model="form.idcard" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="phone" label="手机">
                    <el-input v-model="form.phone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="avatar" label="证件照">
                    <el-upload action="http://localhost:9090/file/upload" ref="img"
                               :on-success="handleImgUploadSuccess">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item prop="representUnit" label="代表单位">
                    <el-input v-model="form.representUnit" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="workUnit" label="工作单位">
                    <el-input v-model="form.workUnit" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="judgeLevel" label="现裁判等级">
                    <el-input v-model="form.judgeLevel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="passDate" label="现等级通过时间">
                    <el-date-picker v-model="form.passDate" type="date" value-format="yyyy-MM-dd"
                                    placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item prop="latestRecord" label="最近执裁或复训活动名称及地点">
                    <el-input v-model="form.latestRecord" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="执裁记录" :visible.sync="dialogTableVisible">
            <el-table :data="recordData">
                <el-table-column property="raceName" label="赛事名称" width="150"></el-table-column>
                <el-table-column property="raceStation" label="分站" width="200"></el-table-column>
                <el-table-column property="raceLevel" label="赛事级别"></el-table-column>
                <el-table-column property="racePost" :formatter="postFormatter" label="担任职位"></el-table-column>
            </el-table>
        </el-dialog>

        <el-dialog title="生成年度执裁记录" :visible.sync="dialogJudgeVisible" width="40%" :close-on-click-modal="false">
            <el-form label-width="120px" size="small" style="width: 80%; margin: 0 auto"
                     :rules="rules" :model="ruleForm" ref="rankForm">
                <el-form-item prop="raceYear" label="赛事年份">
                    <el-date-picker v-model="ruleForm.raceYear" value-format="yyyy" type="years"
                                    @change="queryRaces(ruleForm.raceYear)">
                    </el-date-picker>
                </el-form-item>
                <el-form-item prop="race" label="赛事名称">
                    <el-select v-model="ruleForm.rankRace" multiple
                               collapse-tags placeholder="请选择">
                        <el-option
                                v-for="item in raceNames"
                                :key="item.raceId"
                                :label="item.raceName+'('+item.raceStation+')'"
                                :value="item.raceId">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogJudgeVisible = false">取 消</el-button>
                <el-button type="primary" @click="generateJudges(ruleForm)">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import axios from "axios";

    export default {
        name: "Judge",
        data() {
            return {
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 10,
                name: "",
                form: {},
                dialogTableVisible: false,
                dialogFormVisible: false,
                dialogPostFormVisible: false,
                dialogJudgeVisible: false,
                multipleSelection: [],
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                post: "",
                postData: {},
                recordData: [],//执裁记录,
                raceDate: '',
                userIdcard: "",
                raceNames: [],
                ruleForm: {
                    raceYear: '',
                    rankRace: [],
                },
                rules: {
                    raceYear: [
                        {required: true, message: '请选择赛事年份', trigger: 'change'}
                    ],
                    rankRace: [
                        {required: true, message: '请选择比赛', trigger: 'change'},
                    ]
                },
            }
        },
        created() {
            this.load()
        },
        methods: {
            load() {
                const data = {
                    "page": this.pageNum,
                    "pageSize": this.pageSize,
                    "name": this.name,
                }
                this.request.post("/judge/page", data).then(res => {
                    //this.tableData = res.data.records
                    this.tableData = res.data.data
                    this.total = res.data.total
                })
            },
            loadRecord() {
                const data = {
                    "raceDate": this.raceDate,
                    "userIdcard": this.userIdcard
                    // "raceName":this.raceName
                }
                this.request.post("/judge/queryRecord", JSON.stringify(data)).then(res => {
                    if (res.code === 200) {
                        this.recordData = res.data
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            postFormatter(row) {
                let post = "";
                //兼容执裁记录的字段racePost
                if (row.post === undefined) {
                    post = row.racePost
                } else {
                    post = row.post
                }
                switch (post) {
                    case "0":
                        return "技术代表"
                        break;
                    case "1":
                        return "仲裁"
                        break;
                    case "2":
                        return "裁判长"
                        break;
                    case "3":
                        return "副裁判长"
                        break;
                    case "4":
                        return "裁判员"
                        break;
                }
            },
            acceptFormatter(row) {
                if (row.isAccept === "0") {
                    return "同意"
                } else if (row.isAccept === "1") {
                    return "拒绝"
                }
            },
            sexFormatter(row) {
                if (row.sex === "0") {
                    return "男"
                } else {
                    return "女"
                }
            },
            levelFormatter(row) {
                switch (row.judgeLevel) {
                    case "0":
                        return "国际级"
                        break;
                    case "1":
                        return "国家级"
                        break;
                    case "2":
                        return "预备国家级"
                        break;
                    // case "3": return "二级"
                    //   break;
                    // case "4": return "三级"
                    //   break;
                }
            },
            /**
             * 查询赛事
             * @param rankYear
             */
            queryRaces(rankYear) {
                if (rankYear !== null && rankYear.length > 0) {
                    const data = {
                        "rankYear": rankYear
                    }
                    this.request.post("/rankCategory/queryRaces", data).then(res => {
                        this.raceNames = res.data
                    })
                }

            },
            generateRecord() {
                this.dialogJudgeVisible = true;
                this.ruleForm = {}
            },
            generateJudges(data) {
                console.log(data)
                this.dialogJudgeVisible = false;
                axios({
                    method: 'post',
                    url: this.GLOBAL.baseURL + '/rank/judge/exportJudges',
                    data: {
                        "races": data.rankRace,//上传的参数
                    },
                    headers: {
                        // Authorization: 'Bearer ' + this.$store.getters.token
                    },
                    responseType: 'blob' //非常重要不可少
                }).then(res => {
                    if (res.status === 200) {
                        const url = window.URL.createObjectURL(new Blob([res.data]))//返回的数据
                        const link = document.createElement('a')
                        link.style.display = 'none'
                        link.href = url
                        link.setAttribute('download', '年度执裁记录表.xlsx')//文件名
                        document.body.appendChild(link)
                        link.click()
                        link.remove()
                    } else {
                        this.$message.error("生成失败")
                    }
                })
            },
            save() {
                this.request.post("/judge/save", this.form).then(res => {
                    if (res.code === 200) {
                        this.$message.success(res.message)
                        this.dialogFormVisible = false
                        this.load()
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            /**
             * 查询执裁记录
             * @param row
             */
            queryRecord(row) {
                this.dialogTableVisible = true
                const data = {
                    "raceDate": this.raceDate,
                    "userIdcard": row.idcard
                    // "raceName":this.raceName
                }
                this.request.post("/judge/queryRecord", JSON.stringify(data)).then(res => {
                    if (res.code === 200) {
                        this.recordData = res.data
                        this.userIdcard = res.data[0].userIdcard
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },

            reset() {
                this.name = ""
                this.load()
            },
            recordReset() {
                this.raceDate = ""
                // this.raceName=""
                this.loadRecord()
            },
            handleSizeChange(pageSize) {
                console.log(pageSize)
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                console.log(pageNum)
                this.pageNum = pageNum
                this.load()
            },
            handleFileUploadSuccess(res) {
                this.form.file = res
            },
            handleImgUploadSuccess(res) {
                this.form.img = res
            },
            download(url) {
                window.open(url)
            },
            exp() {
                window.open("http://localhost:9090/judge/export")
            },
            handleExcelImportSuccess() {
                this.$message.success("导入成功")
                this.load()
            }
        }
    }
</script>


<style>
    .headerBg {
        background: #eee !important;
    }

    .el-button {
        margin: 0px 6px 12px;
    }

    .el-icon {
        font-size: 16px;
    }
</style>
